<?php
// +----------------------------------------------------------------------
// | Description: 系统操作日志
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------

namespace app\admin\model;

use app\common\model\Common;

class SignLog extends Common
{
    protected $name = 'admin_signlog';
    protected $createTime = 'login_time';
    protected $updateTime = false;
    protected $autoWriteTimestamp = true;
    protected $insert = [
        'status' => 1,
    ];
    protected $page; // 当前页码
    protected $limit = 2; // 每页数据

    // 本表查询
    protected $fuzzyConditions = ['ip']; // 精确查询字段
    protected $rangeConditions = ['login_time']; // 范围查询字段
    protected $clearConditions = null; // 范围查询字段

    // 联合查询
    protected $union_fuzzyConditions = [
        ['admin_user', 'username'],
    ];
    protected $foreignConditions = [
        'admin_user' => ['id', 'uid'],
    ]; // 联合查询  key 为联合表名称 value 两个字段为联合查询的重合字段

    protected $returnFields = 'admin_signlog.*,admin_user.username';

    /**
     * 查询某个用户的日志列表
     * @param $uid  用户id
     * @param $page 页码
     * @param $limit 每页条目
     */
    public function getDataByUId($page, $limit)
    {
        $dataCount = $this->count();
        // 若有分页
        if ($page && $limit) {
            $list = $this->page($page, $limit);
            $listData = $list
                ->alias('log')
                ->join('__ADMIN_USER__ user', 'log.uid=user.id', 'LEFT')
                ->field('log.*, user.username as username')
                ->select();
        } else {
            $listData = $this
                ->alias('log')
                ->join('__ADMIN_USER__ user', 'log.uid=user.id', 'LEFT')
                ->field('log.*, user.username as username')
                ->select();
        }

        $data['list'] = $listData;
        $data['dataCount'] = $dataCount;

        return $data;
    }

    public function delDataById($id = '', $delSon = false)
    {
        return parent::delDataById($id, $delSon); // TODO: Change the autogenerated stub
    }

    /**
     * [delDatas 批量删除数据]
     * @linchuangbin
     * @DateTime  2017-02-11T20:59:34+0800
     * @param     array                   $ids    [主键数组]
     * @param     boolean                 $delSon [是否删除子孙数据]
     * @return    [type]                          [description]
     */
    public function delDatas($ids = [], $delSon = false)
    {
        try {
            $this->where($this->getPk(), 'in', $ids)->delete();
            return true;
        } catch (\Exception $e) {
            $this->error = '操作失败';
            return false;
        }
    }
}
